<?php
declare (strict_types = 1);

namespace app\controller;

use think\Request;
use think\facade\Db;
class Role
{
    /**
     * 显示资源列表
     *
     * @return \think\Response
     */
    public function index()
    {
        //
		$res = Db::query("select * from role_admin");
		return json(["code"=>200,"data"=>$res]);
    }
	public function findByUserId(){
		$p = input("get.");
		$res = Db::name("user_role_admin")->where("userId",$p["id"])->select();
		return json(["code"=>200,"data"=>$res]);
	}
    /**
     * 显示创建资源表单页.
     *
     * @return \think\Response
     */
    public function create()
    {
        //
		$p = input('post.');
		$date = date('Y-m-d H:i:s',time());
		$data = ["name"=>$p["name"],"createTime"=>$date,"updateTime"=>$date];
		Db::startTrans();
		try{
			$rid = Db::name("role_admin")->insertGetId($data);
			Db::execute("insert into role_permission_admin (roleId,permissionId,createTime,updateTime) values (?,?,?,?)",[$rid,$p["permission"],$date,$date]);
			foreach($p["menuIds"] as $id){
				Db::execute("insert into role_menu_admin (roleId,menuId,createTime,updateTime) values (?,?,?,?)",[$rid,$id,$date,$date]);
			}
			Db::commit();
		}catch (\Exception $e) {
			  // 回滚事务
			Db::rollback();
		}
		return json(["code"=>200,"msg"=>"操作成功"]);
    }

    /**
     * 保存新建的资源
     *
     * @param  \think\Request  $request
     * @return \think\Response
     */
    public function save(Request $request)
    {
        //
    }

    /**
     * 显示指定的资源
     *
     * @param  int  $id
     * @return \think\Response
     */
    public function read($id)
    {
        //
    }

    /**
     * 显示编辑资源表单页.
     *
     * @param  int  $id
     * @return \think\Response
     */
    public function edit($id)
    {
        //
    }

    /**
     * 保存更新的资源
     *
     * @param  \think\Request  $request
     * @param  int  $id
     * @return \think\Response
     */
    public function update()
    {
        //
		$p = input("post.");
		Db::startTrans();
		try{
			Db::name("role_admin")->update(["id"=>$p["id"],"name"=>$p["name"]]);
			Db::name("role_permission_admin")->where("roleId",$p["id"])->update(["permissionId"=>$p["permission"]]);
			Db::name("role_menu_admin")->where("roleId",$p["id"])->delete();
			$date = date('Y-m-d H:i:s',time());
			foreach($p["checkList"] as $value){
				$data = ["roleId"=>$p["id"],"menuId"=>$value,"createTime"=>$date,"updateTime"=>$date];
				Db::name("role_menu_admin")->insert($data);
			}
			Db::commit();
		}catch(\Exception $e){
			Db::rollback();
		}
    }

    /**
     * 删除指定资源
     *
     * @param  int  $id
     * @return \think\Response
     */
    public function delete()
    {
        //
		$p = input("get.");
		Db::startTrans();
		try{
			Db::name("role_admin")->where("id",$p["id"])->delete();
			Db::name("role_permission_admin")->where("roleId",$p["id"])->delete();
			Db::name("role_menu_admin")->where("roleId",$p["id"])->delete();
			Db::commit();
		}catch(\Exception $e){
			Db::rollback();
		}
		return json(["code"=>200,"msg"=>"操作成功"]);
    }
}
